package com.project.finance.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.finance.common.mapper.TbUsersMapper;
import com.project.finance.common.po.TbUsers;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUsersMapper tbUsersMapper;

    @Override
    public List<TbUsers> selectAll() {
        return tbUsersMapper.selectAll();
    }

    @Override
    public int insert(TbUsers tbUsers) {
        return tbUsersMapper.insert(tbUsers);
    }

    @Override
    public int update(TbUsers tbUsers) {
        return tbUsersMapper.updateByPrimaryKey(tbUsers);
    }

    @Override
    public int delete(int userId) {
        return tbUsersMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public ResultMsg listUsersByPage(int pageSize, int pageNum) {
        ResultMsg resultMsg = new ResultMsg();

        PageHelper.startPage(pageNum, pageSize);

        List<TbUsers> tbUsers = tbUsersMapper.selectAll();

        //获取结果集的分页信息
        PageInfo pageInfo = new PageInfo(tbUsers);
        //获取总记录数
        long total = pageInfo.getTotal();
        resultMsg.setTatol(total);
        resultMsg.setData(tbUsers);
        return resultMsg;
    }

    @Override
    //根据账号来查询用户信息
    public List<TbUsers> getUser(String username) {
        Example example = new Example(TbUsers.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("userName","%"+username+"%");
        List<TbUsers> tbUsers=tbUsersMapper.selectByExample(example);
        return tbUsers;

    }
}
